package managedbean.SaleLead; 
 
import entity.CRM.Representative;
import entity.CRM.SaleLead.CustomerType;
import entity.Employee;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB; 
import javax.el.ELContext;
import javax.faces.application.FacesMessage; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext; 
import javax.faces.event.ActionEvent; 
import managedbean.Controller;
import session.stateless.CRM.SaleLeadBeanLocal;
 
@ManagedBean(name="saleLeadManagerBean")
@ViewScoped
public class AddSaleLeadManagerBean implements Serializable { 
    @EJB 
    private SaleLeadBeanLocal saleLeadBeanLocal; 
    
    private String registrationNumber;
    private String saleLeadName; 
    private String address;
    private CustomerType customerType;
    private String statusMessage;

    private List<Representative> reps = new ArrayList<Representative>();
    private Representative newRep = new Representative();
    
    public String getSaleLeadName() { 
        return saleLeadName; 
    } 
    
    public void setSaleLeadName(String saleLeadName) { 
        this.saleLeadName = saleLeadName; 
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    public CustomerType getCustomerType() {
        return customerType;
    }
    public void setCustomerType(CustomerType customerType) {
        this.customerType = customerType;
    }
    
    public String getStatusMessage() { 
        return statusMessage; 
    } 
    public void setStatusMessage(String statusMessage) { 
        this.statusMessage = statusMessage; 
    }

    public String getRegistrationNumber() {
        return registrationNumber;
    }

    public void setRegistrationNumber(String registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    public SaleLeadBeanLocal getSaleLeadBeanLocal() {
        return saleLeadBeanLocal;
    }

    public void setSaleLeadBeanLocal(SaleLeadBeanLocal saleLeadBeanLocal) {
        this.saleLeadBeanLocal = saleLeadBeanLocal;
    }

    public List<Representative> getReps() {
        return reps;
    }

    public void setReps(List<Representative> reps) {
        this.reps = reps;
    }

    public Representative getNewRep() {
        return newRep;
    }

    public void setNewRep(Representative newRep) {
        this.newRep = newRep;
    }

   
    
    public void saveNewSaleLead(ActionEvent event) 
    {
        String temp_RegistrationNumber = String.valueOf(registrationNumber);
        String temp_Name = String.valueOf(saleLeadName);
        String temp_address = String.valueOf(address);
        CustomerType temp_CustomerType = customerType;
        List<Representative> temp_reps = reps;
        if (reps.size()>0)
        
        try 
        { 
            List<Representative> temps = new ArrayList<Representative>();
            for (int i=0; i < temp_reps.size(); i++)
            {
                Representative temp = saleLeadBeanLocal.createRepresentative(temp_reps.get(i).getName(), temp_reps.get(i).getEmail(), temp_reps.get(i).getContactNumber());
                temps.add(temp);
            }
            
            ELContext elContext = FacesContext.getCurrentInstance().getELContext();
            Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
            Employee executive = controller.getEmployee();
            saleLeadBeanLocal.createSaleLead(temp_Name, temp_RegistrationNumber, temp_address, temp_CustomerType, temps, executive.getEmployeeId());
            statusMessage = "New Sale Lead Saved Successfully"; 
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Add New Sale Lead Result: " + statusMessage + ")", "")); 
        } 
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
    }
    
    public void addRep(ActionEvent event)
    {
        Representative temp = this.newRep;
        reps.add(temp);
        System.out.println("after add rep"+reps.size());
        System.out.println(reps.get(0).getEmail());
        newRep = new Representative();
    }
    
         
}
    

